package com.cn.qtms.service.impl;//类注释设置模板

import com.cn.qtms.base.dao.BaseDao;
import com.cn.qtms.base.service.impl.BaseServiceImpl;
import com.cn.qtms.dao.TmGoodsPriceDao;
import com.cn.qtms.entity.TmGoods;
import com.cn.qtms.entity.TmGoodsPrice;
import com.cn.qtms.exception.AppException;
import com.cn.qtms.service.TmGoodsPriceService;
import com.cn.qtms.service.TmGoodsService;
import com.cn.qtms.service.codename.CodeNameService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Name:TmGoodsPriceServiceImpl
 * @Description:
 * @Author: zhangyang
 * @Date: 2019-12-25 19:20
 **/
@Service
public class TmGoodsPriceServiceImpl extends BaseServiceImpl<TmGoodsPrice, Long> implements TmGoodsPriceService {
    @Autowired
    private TmGoodsPriceDao dao;
    @Autowired
    private TmGoodsService goodsService;

    @Override
    public BaseDao<TmGoodsPrice, Long> getDao() {
        return dao;
    }

    @Override
    public List<TmGoodsPrice> findAllByGoodsId(Long goodsId) {
        return dao.findAllByGoodsId(goodsId);
    }

    @Override
    public void afterCreate(TmGoodsPrice model, TmGoodsPrice tmGoodsPrice) {
        final List<TmGoodsPrice> all = this.findAllByGoodsId(model.getGoodsId());
        if (all == null || all.size() == 0) {
            throw new AppException("当前商品价格为空");
        }
        //设置商品的最低价格和最低会员等级
        final TmGoods goods = goodsService.findById(model.getGoodsId());
        goods.setMinUserLevel(all.get(0).getUserLevel());
        goods.setSalePrice(all.get(0).getSalePrice());
        goodsService.save(goods);
        super.afterCreate(model, tmGoodsPrice);
    }
}
